window.main = window.main || {};

qinglan.Canvas.World.globalRes.loadImg(qinglan.Canvas.Config.resource.img, function(img, percent, isload) {
	console.log(img.src + ' ' + percent + ' ' + isload);
	percent == 1 && main.init();
	
});

main.layer = new qinglan.Canvas.Layer('main', 1);

main.pre = function() {

};

main.init = function() {
	var aimg_stay = new qinglan.Canvas.Img('hakurei', new qinglan.Canvas.Vector2(10, 10), {
		imgNode: qinglan.Canvas.World.globalRes.getImgById('hakurei_action.png'),
		view: new qinglan.Canvas.Vector2(32, 47)
	});
	var animImg = new qinglan.Canvas.movieClip(aimg_stay,{
		frame: 8,
		warp: new qinglan.Canvas.Vector2(32, 0),
		durationFrames: 35
	});
	//main.layer.addSprite(aimg_stay);
	main.layer.addSprite(animImg);
	qinglan.Canvas.World.frm.addLayer(main.layer);
	var moveVect = {
		37: new qinglan.Canvas.Vector2(-3, 0),
		38: new qinglan.Canvas.Vector2(0, -3),
		39: new qinglan.Canvas.Vector2(3, 0),
		40: new qinglan.Canvas.Vector2(0, 3)

	};
	var move = {
		37: false,
		38: false,
		39: false,
		40: false
	};
	animImg.implement('update',function(ctx){
			this.change(ctx);
			qinglan.Object.each(move,function(ismov,index){
				//(true == ismov) && (this.qinglanImg.position = this.qinglanImg.position.add(moveVect[index]));
				if(true == ismov){
					this.qinglanImg.position = this.qinglanImg.position.add(moveVect[index]);
				}
			},this);
		});
 

	qinglan.Canvas.EventCollection.on(aimg_stay,'keydown',function(e,keyMap){
		move[e.keyCode] = true;
		return false;
	},function(e,keyMap){
		return  keyMap['up'] == e.keyCode || keyMap['down'] == e.keyCode || keyMap['left'] == e.keyCode || keyMap['right'] == e.keyCode;
	});
	qinglan.Canvas.EventCollection.on(aimg_stay,'keyup',function(e,keyMap){
		move[e.keyCode] = false;
		console.log(e.keyCode);
		return false;
	},function(e,keyMap){
		return  keyMap['up'] == e.keyCode || keyMap['down'] == e.keyCode || keyMap['left'] == e.keyCode || keyMap['right'] == e.keyCode;
	});

	qinglan.Canvas.World.frm.start();
};
